﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HurlBat.XEdit2D
{
    public partial class Node : UserControl
    {
        DiagramPanel container;
        protected List<Arrow> arrows = new List<Arrow>();

        Point mouseDragStart = Point.Empty;
        Point initialLocation = Point.Empty;
        bool isDragged = false;
        bool isDrawingArrow = false;
        Point initialArrowLocation = Point.Empty;

        public string Title { get { return this.lblTitle.Text; } set { this.lblTitle.Text = value; } }
        
        protected Color borderColor;
        public Color BorderColor { get { return borderColor; } set { borderColor = value; } }

        Color focusedBackColor, unfocusedBackColor;
        public override Color BackColor { get { return focusedBackColor; } set { focusedBackColor = value; } }

        public Node() : this(null, null, string.Empty, Color.White, Color.Gray, Color.Black) { }

        public Node(DiagramPanel container, Image icon, string title, Color backColor, Color borderColor, Color foreColor)
        {
            InitializeComponent();
            this.container = container;
            this.imgIcon.Image = icon;
            this.lblTitle.Text = title;
            this.borderColor = borderColor;
            this.focusedBackColor = backColor;
            this.unfocusedBackColor = Color.FromArgb(150, this.focusedBackColor);
            this.ForeColor = foreColor;

            AddContextMenuItem("arrowToolStripMenuItem", "Arrow", new EventHandler(this.arrowToolStripMenuItem_Click));
            AddContextMenuSeparator();
        }

        protected void AddContextMenuItem(string name, string text, EventHandler clickHandler)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            item.Name = name;
            item.Text = text;
            item.Click += clickHandler;
            this.contextMenu.Items.Add(item);
        }

        protected void AddContextMenuSeparator()
        {
            ToolStripSeparator item = new ToolStripSeparator();
            this.contextMenu.Items.Add(item);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Pen p = new Pen(this.borderColor, 1);
            SolidBrush b = new SolidBrush(this.ForeColor);
            
            e.Graphics.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                isDragged = true;
                mouseDragStart = System.Windows.Forms.Control.MousePosition;
                initialLocation = this.Location;
                this.BringToFront();
                if (!this.Focused) this.Focus();
                this.container.EndArrow(this);
                Cursor.Current = Cursors.SizeAll;
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenu.Items["arrowToolStripMenuItem"].Enabled = this.AllowOutput();
                contextMenu.Show(System.Windows.Forms.Control.MousePosition);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isDragged)
            {
                this.Left = initialLocation.X + System.Windows.Forms.Control.MousePosition.X - mouseDragStart.X;
                this.Top = initialLocation.Y + System.Windows.Forms.Control.MousePosition.Y - mouseDragStart.Y;
                this.Parent.Refresh();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (!isDragged) return;
            isDragged = false;
            mouseDragStart = Point.Empty;
            initialLocation = Point.Empty;
            Cursor.Current = Cursors.Default;
        }

        protected override void OnGotFocus(System.EventArgs e)
        {
            this.BackColor = this.focusedBackColor;
        }

        protected override void OnLostFocus(System.EventArgs e)
        {
            this.BackColor = this.unfocusedBackColor;
        }

        private void arrowToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            this.container.StartArrow(this, System.Windows.Forms.Control.MousePosition);
        }

        public virtual bool AddInput(Node input)
        {
            return false;
        }

        public virtual bool AllowOutput()
        {
            return true;
        }

        public void AddArrow(Arrow arrow)
        {
            arrows.Add(arrow);
        }

        public virtual void ProcessInput()
        {
        }
    }
}
